package org.jsola.contract.helper;

import org.jsola.common.SafeKit;
import org.jsola.contract.dto.VariableAddDTO;
import org.jsola.contract.dto.VariableUpdateDTO;
import org.jsola.contract.entity.VariableDO;
import org.jsola.contract.query.VariableQuery;
import org.jsola.contract.service.IVariableService;
import org.jsola.contract.vo.VariableVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

import static org.jsola.contract.core.AbstractTest.SITE_ID;
import static org.jsola.user.constant.UserConstants.DEFAULT_SITE_ID;


/**
 * 模板变量
 *
 * @author JanusMix
 */
@Component
public class VariableTestHelper extends BaseTestHelper {
    
    @Autowired
    private IVariableService variableService;

    /**
     * 生成add测试数据
     * @return VariableAddDTO
     */
    public VariableAddDTO getVariableAddDTO(){
        VariableAddDTO variableAddDTO = new VariableAddDTO();
        variableAddDTO.setName(getRandomValue("varchar",255));
        variableAddDTO.setType(getRandomValue("varchar",255));
        variableAddDTO.setNeed(SafeKit.getBoolean(getRandomValue("tinyint",1)));
        return variableAddDTO;
    }


    /**
     * 生成update测试数据
     * @return VariableUpdateDTO
     */
    public VariableUpdateDTO getVariableUpdateDTO(VariableVO variableVO){
        return variableService.selectDOById(variableVO.getId()).to(VariableUpdateDTO.class);
    }

    /**
     * 生成查询数据
     * @return VariableQuery
     */
    public VariableQuery getVariableQuery() {
        VariableQuery variableQuery = new VariableQuery();
        List<VariableDO> variableDOList = variableService.list(new VariableDO(),DEFAULT_SITE_ID);
        if (!CollectionUtils.isEmpty(variableDOList)) {
            VariableDO variableDO = variableDOList.get(0);
            variableQuery = variableDO.to(VariableQuery.class);
        }
        return variableQuery;
    }

    /**
     * 保存示例
     * @return VariableVO
     */
    public VariableVO saveVariableExample(){
        VariableAddDTO addDTO = this.getVariableAddDTO();
        VariableVO variableVO = variableService.save(addDTO,buildTokenUser());
        return variableVO;
    }


}

